﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MMITPinGen.Entities;
using MMITCore.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Web.Security;
using MMITCore;
using Microsoft.ApplicationBlocks.Data;
using MMITDomain;
using MMITDomain.Concrete;
using MMITDomain.Entities;


namespace MMITPinGen
{
    public partial class InjectPINsToDB1 : System.Web.UI.Page
    {
        EFPartnerRepository PartnerRepository = new EFPartnerRepository();
        EFPINRepository PINRepository = new EFPINRepository();
        EFBatchRepository BatchRepository = new EFBatchRepository();
        SynkronLogger Log = new SynkronLogger("MMITPINGen");
        Dictionary<string, object> d = new Dictionary<string, object>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<string> roles = Roles.GetRolesForUser(User.Identity.Name).ToList();
                var r = from p in roles
                        where p == "MMIT Admin"
                        select p;
                if (r.Count() == 0)
                {
                    lblMsg.Text = "You do not have permission to perform this function. Please contact Customer Service";
                    grdBatches.Visible = false;
                }

                List<Batch> bat = (from p in BatchRepository.Batches
                                   where p.Status == 2
                                   select p).ToList();
                if (bat.Count() > 0)
                {
                    grdBatches.DataSource = bat;
                    grdBatches.DataBind();
                    lblMsg.Text = "To Upload Batch of PINs to Database, Click On It";
                }
                else
                {
                    lblMsg.Text = "No Batches Available to Be Uploaded to MMIT Production Database";
                }

            }
           
        }

        protected void grdBatches_SelectedIndexChanged(object sender, EventArgs e)
        {
            var t = grdBatches.SelectedItems;
            HiddenField k = (HiddenField)t[0].FindControl("HiddenField1");
            Int32 BatchId =Convert.ToInt32(k.Value);
           // 
            Batch b = BatchRepository.GetBatchById(Convert.ToInt32(k.Value));
            if (b.Status == 3)
            {
                lblMsg.Text = "This Batch has Been Uploaded Already";
                HttpContext.Current.Response.End();

            }
            List<PIN> PINs  =  (from p in PINRepository.PINs
                               where p.BatchId ==  BatchId
                               select p).ToList();

            foreach (PIN p in PINs)
            {
                SqlHelper.ExecuteNonQuery(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString, "DAPO_InsertPIN", p.PINNumber, p.SerialNo, p.Value,99999, 0);
            }
            
            b.Status = 3;
            b.DateGenerated= DateTime.Now;
            b.GeneratedBy = User.Identity.Name;
            BatchRepository.SaveBatch(b);

            List<Batch> bat = (from p in BatchRepository.Batches
                               where p.Status == 2
                               select p).ToList();
            if (bat.Count() > 0)
            {
                grdBatches.DataSource = bat;
                grdBatches.DataBind();
                lblMsg.Text = "To Upload Batch of PINs to Database, Click On It";
            }
            else
            {
                lblMsg.Text = "No Batches Available to Be Uploaded to MMIT Production Database";
            }
            
        }

        protected void lnkNew_Click(object sender, EventArgs e)
        {

        }
    }
}